package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.CatalogVO;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
/**
* 攻略分类服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository repsitory;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        //根据目的地id查询目的地对象
        Destination destination = destinationService.get(strategyCatalog.getDestId());
        //设置目的地名称到分类对象中
        strategyCatalog.setDestName(destination.getName());
        repsitory.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        repsitory.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,pageable, query,StrategyCatalog.class, qo);
    }

    @Override
    public List<CatalogVO> queryCatalogs() {
        List<CatalogVO> catalogVOS = new ArrayList<>();
        List<Destination> list = destinationService.list();
        //把需要的数据封装到catalogVo的对象上
        List<StrategyCatalog> list1 = this.list();
        for (Destination destination : list) {
            for (StrategyCatalog strategyCatalog : list1) {
                if (strategyCatalog.getDestName().equals(destination.getName())) {
                    CatalogVO catalogVO = new CatalogVO();
                    catalogVO.setDestName(destination.getName());
                    catalogVO.setCatalogList(repsitory.findByDestName(destination.getName()));
                    catalogVOS.add(catalogVO);
                    break;
                }
            }
        }
        return catalogVOS;
    }

    @Override
    public List<StrategyCatalog> findByDestId(String destId) {
        List<StrategyCatalog> catalogs = repsitory.findByDestId(destId);
        for (StrategyCatalog catalog : catalogs) {
            List<Strategy> list = strategyService.queryByCatalogId(catalog.getId());
            catalog.setStrategies(list);
        }
        return catalogs;
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if(StringUtils.hasLength(strategyCatalog.getId())){
            Destination destination = destinationService.get(strategyCatalog.getDestId());
            //设置目的地名称到分类对象中
            strategyCatalog.setDestName(destination.getName());
            //update
            this.update(strategyCatalog);
        }else{
            //save
            this.save(strategyCatalog);
        }
    }
}
